<template>
  <div class="flex-container">
    <div class="flex-item">
      <div class="header" style="height:128px;">
        <img src="@/assets/img/logo-circle.png" class="system-logo"><span class="title-text">国家医疗保障局医疗保障信息平台</span>
      </div>
    </div>
    <div class="flex-item main">
      <div class="login-panel">
        <img :style="styleObj" alt="" src="img/login-panel-icon.svg" class="login-panel-icon">
        <div class="login-panel-main">
          <el-tabs v-model="activeName">
            <el-tab-pane label="密码登录" name="first">
              <el-form
                ref="loginForm"
                :model="loginForm"
                :rules="loginRules"
                class="login-form"
                auto-complete="on"
                label-position="left"
              >
                <el-form-item prop="username">
                  <span class="svg-container">
                    <!-- <svg-icon icon-class="user" /> -->
                    <i class="el-icon-user-solid"/>
                  </span>
                  <el-input
                    ref="username"
                    v-model="loginForm.username"
                    placeholder="用户名/手机号码/身份证/邮箱"
                    name="username"
                    type="text"
                    tabindex="1"
                    auto-complete="on"
                    autofocus="autofocus"
                  />
                </el-form-item>

                <el-form-item prop="password">
                  <span class="svg-container">
                    <i class="el-icon-lock"/>
                    <!-- <svg-icon icon-class="password" /> -->
                  </span>
                  <el-input
                    ref="password"
                    :key="passwordType"
                    v-model="loginForm.password"
                    :type="passwordType"
                    placeholder="密码"
                    name="password"
                    tabindex="2"
                    auto-complete="on"
                    @keyup.enter.native="handleLogin"
                  />
                  <span class="show-pwd" @click="showPwd">
                    <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
                  </span>
                </el-form-item>
                <el-form-item prop="verifiCode">
                  <!-- <div style="width: 186px; display: inline-block; left: 0px; height: 44px;"> -->
                  <!-- <div style="width: 186px; display: inline-block; left: 0px; height: 44px;"> -->
                  <span class="svg-container">
                    <i class="el-icon-picture-outline-round"/>
                    <!-- <svg-icon icon-class="password" /> -->
                  </span>
                  <el-input
                    v-model="loginForm.verifiCode"
                    style="width: 186px; display: inline-block; left: 0px; height: 44px;"
                    name="verificationCode"
                    tabindex="3"
                    maxlength="4"
                    @keyup.enter.native="handleLogin"
                  />
                  <!-- </div> -->
                  <div :style="styleObject" class="verificationCodeImage" @click="getVerifiCode" />
                </el-form-item>

                <el-button
                  :loading="loading"
                  type=""
                  style="width:100%;margin-bottom:30px;background-color: #549bf6;color:#fff;height:44px;"
                  @click.native.prevent="handleLogin"
                >登录</el-button>
              </el-form>
            </el-tab-pane>
            <el-tab-pane label="CA登录" name="second">CA登录</el-tab-pane>
            <el-tab-pane label="扫码登录" name="third">扫码登录</el-tab-pane>
          </el-tabs>
        </div>
      </div>
    </div>
    <div class="flex-item">
      <div style="height:100px;line-height:70px;">
        Copyright @2020 All right reserved 版权所有 国家医疗保障局
      </div>
    </div>
  </div>
</template>

<script>
// import {
//   validUsername
// } from '@/utils/validate'
import { verifiCode } from '@/api/user'
import Crypto from '@/utils/mix-code'
import {
  mapGetters
} from 'vuex'
export default {
  name: 'Login',
  data() {
    // const validateUsername = (rule, value, callback) => {
    //   if (!validUsername(value)) {
    //     callback(new Error('Please enter the correct user name'))
    //   } else {
    //     callback()
    //   }
    // }
    // const validatePassword = (rule, value, callback) => {
    //   if (value.length < 6) {
    //     callback(new Error('The password can not be less than 6 digits'))
    //   } else {
    //     callback()
    //   }
    // }
    return {
      verifiCodeId: '',
      verifiCodeSrc: '',
      activeName: 'first',
      styleObject: '',
      styleObj: '',
      loginForm: {
        username: '',
        password: '',
        verifiCode: ''
      },
      loginRules: {
        username: [{
          required: true,
          trigger: 'blur',
          message: '请输入用户名'
        }],
        password: [{
          required: true,
          trigger: 'blur',
          message: '请输入密码'

        }],
        verifiCode: [{
          required: true,
          trigger: 'blur',
          message: '请输入验证码'
        }]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined,
      client_id: 'acme1'
    }
  },
  computed: {
    ...mapGetters([
      'token'
    ])
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  created() {
  },
  mounted() {
    this.$nextTick(() => {
      this.getVerifiCode()
      setTimeout(() => {
        this.styleObj = 'left: 16px; opacity: 1;'
      }, 300)
    })
  },
  destroyed() {
  },
  methods: {
    // 获取验证码
    getVerifiCode() {
      verifiCode(this.verifiCodeId).then(res => {
        if (res.code === 0) {
          this.verifiCodeSrc = res.data.src
          this.styleObject = {
            'background-image': `url("data:image/jpg;base64,${res.data.src}")`
          }
          this.verifiCodeId = res.data.id
        }
      })
    },
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.$store.dispatch('user/GET_TOKEN').then(res => {
            let loginInfo = { ...this.loginForm, verifiCodeId: this.verifiCodeId, client_id: this.client_id }
            const params = Crypto.encrypt(JSON.stringify(loginInfo), this.token)
            // let params = JSON.stringify(loginInfo)
            this.loading = true
            this.$store.dispatch('user/login', params).then(res => {
              // console.log(res.indexOf('登录成功'))
              // if (res.indexOf('登录成功') !== -1) {
              if (res.code === 0) {
                this.$store.dispatch('getMenuList').then(res => {
                  if (res.data && res.data.length > 0) {
                    this.$router.push({
                      path: this.redirect || '/'
                    })
                  } else {
                    this.$alert(`<div class="myalert-header">操作失败</div>
                    <div class="myalert-content">对不起，该用户没有权限，请更换用户登陆</div>`, {
                      dangerouslyUseHTMLString: true, confirmButtonText: '确定',
                      type: 'error'
                    })
                  }
                })
              } else {
                setTimeout(() => {
                  this.verifiCodeId = ''
                  this.loginForm.verifiCode = ''
                  this.getVerifiCode()
                }, 300)
              }
              this.loading = false
            }).catch(() => {
              this.getVerifiCode()
            })
          }).catch(() => {
            this.loading = false
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}

</script>
<style lang="scss">
  /* 修复input 背景不协调 和光标变色 */
  /* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

  $bg:#283443;
  $light_gray:#fff;
  $cursor: #fff;

  @supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
    .login-container .el-input input {
      color: $cursor;

    }
  }

  /* reset element-ui css */
  .flex-container {
    .el-input {
      display: inline-block;
      // width: 65%;
      input {
        background: transparent;
        border: 0px;
        -webkit-appearance: none;
        border-radius: 0px;
        padding: 12px 5px 12px 15px;
        height: 44px;
        // color: $light_gray;
        // caret-color: $cursor;
      }
    }

    .el-form-item {
      border: 1px solid #67c23a;
      // background: rgba(0, 0, 0, 0.1);
      border-radius: 5px;
      color: #454545;
    }
    .el-form-item__content{
      display: flex;
      line-height: 32px;
    }
  }

</style>

<style lang="scss" scoped>
  $bg:#2d3a4b;
  $dark_gray:#889aa4;
  $light_gray:#eee;

  .el-container {
    .is-vertical {
      flex-direction: column;
    }
  }

  .main {
    background: url("../../assets/img/login-bg.png");
    background-repeat: no-repeat;
    background-size: cover;
    background-position: 50%;
  }

  .main,
  .footer {
    display: flex;
    justify-content: center;
    align-items: center;
    position: relative;
  }

  .flex-container {
    height: 100%;
    display: flex;
    flex-direction: column;
    text-align: center;
  }

  .flex-item:nth-child(1),
  .flex-item:nth-child(3) {
    flex-grow: 0;
    flex-shrink: 0;
    background-color: #fff;
  }

  .flex-item:nth-child(2) {
    flex-grow: 1;
    flex-shrink: 1;
    background-color: #000;
    overflow-y: auto;
  }

  .center {
    position: fixed;
    top: 50%;
    left: 50%;
    margin-top: -10px;
    margin-left: -24px;
  }

  .header {
    display: flex;
    justify-content: center;
    align-items: center;
    line-height: 52px;
    font-size: 36px;
    color: #333;

    .system-logo {
      width: 50px;
      margin-right: 24px;
    }
  }

  .main {
    .login-panel {
      width: 834px;
      height: 352px;
      box-shadow: 0 0 8px rgba(39, 116, 234, .38), 2px 2px 16px rgba(19, 70, 128, .55);
      background: url('../../assets/img/login-bg-panel.png');
      background-repeat: no-repeat;
      background-size: 100% 100%;
      position: relative;
      overflow: hidden;

      .login-panel-icon{
        background: url("../../assets/img/login-panel-icon.svg");
        background-repeat: no-repeat;
        top: 40px;
        width: 340px;
        height: 272px;
        position: absolute;
        left: -340px;
        opacity: .2;
        transition: left .6s cubic-bezier(.5, 0, 0, 1), opacity .6s;
      }
    }
        .login-panel-main {
    top: 32px;
    width: 400px;
    height: 288px;
    position: absolute;
    right: 20px;
}
.el-tabs{
      padding: 0 20px 0 20px;
}
.el-tabs .el-tabs__header {
    margin-bottom: 16px!important;
}
/deep/ .el-tabs__nav {
    min-width: 100%;
    display: flex;
    .el-tabs__item{
    flex:1;
    font-size: 16px!important;
  }
  }
 /deep/ .el-input .el-input__inner{
        font-size: 14px;
      }
  }
.el-button.el-button--small{
  font-size: 16px;
}
    .login-form {
          padding: 8px 20px 0 20px;
      // padding: 160px 35px 0;
      margin: 0 auto;
      overflow: hidden;

    }

    .tips {
      font-size: 14px;
      color: #fff;
      margin-bottom: 10px;

      span {
        &:first-of-type {
          margin-right: 16px;
        }
      }
    }

    .svg-container {
      padding: 0 15px 0 15px;
      color: $dark_gray;
      vertical-align: middle;
      width: 50px;
      display: inline-block;
      line-height: 44px;
      font-size: 24px;
      // box-sizing: border-box;
    }

    .title-container {
      position: relative;

      .title {
        font-size: 26px;
        color: $light_gray;
        margin: 0px auto 40px auto;
        text-align: center;
        font-weight: bold;
      }
    }

    .show-pwd {
      position: absolute;
      right: 10px;
      top: 7px;
      font-size: 16px;
      color: $dark_gray;
      cursor: pointer;
      user-select: none;
    }
    .verificationCodeImage {
    width: 120px;
    height: 44px;
    margin-left: 14px;
    background-size: 100% 100%;
    display: inline-block;
    line-height: 44px;
    font-size: 14px;
    text-align: center;
    vertical-align: top;
    border-radius: 4px;
    }
</style>
